package com.ldk.api;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.util.AlipayCore;
import com.ldk.base.framework.util.ConvertUtil;
import com.ldk.base.framework.util.HttpUtil;
import com.ldk.base.framework.util.SignUtil;
import com.ldk.base.framework.util.baidu.RouteMatrixApi;
import com.ldk.base.framework.util.baidu.routematrix.RouteMatrixRequestParms;
import com.ldk.base.framework.util.baidu.routematrix.RouteMatrixResponseParmsResult;
import com.ldk.base.framework.util.sms.AliDayu;
import com.ldk.base.framework.util.sms.SmsConfig;
import com.ldk.base.framework.util.sms.SmsFactory;
import com.ldk.base.framework.util.sms.SmsPlatform;
import com.ldk.framework.entity.Alipay;
import com.ldk.framework.enums.TripRefundStatus;
import com.ldk.base.framework.util.baidu.AddressComponentApi;

/**
 * 接口测试
 *
 * @author xiangwl
 * 2017年2月25日
 * com.ldk.api
 * TestApi
 */
public class TestApi {

    public static void main(String[] args) {
        //String validCode = getValidCode();
        //System.out.println("validCode:" + validCode);

        //userLogin();
        //getHelpDocList();
        //driverLogin();
        //baiduDriving();
        //zxpcUserPublish();
        //searchLostList();
        //searchDriver();
        //driverGetUserNewTripList();
        //zxpcDriverAccept();
        //driverRefuse();
        //userGetTripList();
        //driverGetTripCrewsList();
        //zxpcDriverSureUserOnCar();
        //bcDriverAccept();
        //driverCompleteTrip();

        //estimateFee();

        //zxpcUserCancel();
        //userPayTripDeposit();
        //driverCanAcceptUserTripList();
        //kcUserPublish();
        //userComplaint();
        //userComment();

        //userRecharge();
        //smsCheckExpired();
        //sendSms();
        //setPayPwd();
        //aliRefund();

        //getCarRanks();

        //getAddressComponent();

       // generateID(6);


        importDriver();
    }

    public static void getAddressComponent() {
        String lat = "27.69758461023288";
        String lng = "106.94239059124112";
        JSONObject addressComponent = AddressComponentApi.getAddressComponent(lng, lat, Consts.BAIDU_MAP_AK);
        if (addressComponent != null) {
            System.out.println(addressComponent.getString("province") + "," + addressComponent.getString("city") + "," + addressComponent.getString("district"));
        }
    }


    public static String importDriver() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("phoneNo", "15120197446");
        Map<String, Object> response = requestService("driver", "importDriver", parms);
        if (response == null || response.isEmpty() || !response.containsKey("validCode"))
            return "";

        return ConvertUtil.parseString(response.get("validCode"));
    }


    public static String getValidCode() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("phoneNo", "15120197446");
        Map<String, Object> response = requestService("sms", "getValidCode", parms);
        if (response == null || response.isEmpty() || !response.containsKey("validCode"))
            return "";

        return ConvertUtil.parseString(response.get("validCode"));
    }

    public static void userLogin() {
        Map<String, Object> parms = new HashMap<String, Object>();
        //parms.put("phoneNo", "15285566816");
        parms.put("phoneNo", "15120197446");

        parms.put("validCode", "774552");
        parms.put("clientInfo", "ios iphone 7");
        parms.put("lon", "106.42");
        parms.put("lat", "26.35");
        Map<String, Object> response = requestService("user", "login", parms);
        System.out.print("response=" + response);
    }

    public static void expressPublish() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("phoneNo", "15285566816");
        parms.put("validCode", "774552");
        parms.put("clientInfo", "ios iphone 7");
        parms.put("lon", "106.42");
        parms.put("lat", "26.35");
        Map<String, Object> response = requestService("express", "publish", parms);
        System.out.print("response=" + response);
    }

    public static void getHelpDocList() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("platform", "2");
        parms.put("typeId", "2");
        Map<String, Object> response = requestService("help", "getHelpDocList", parms);
        System.out.print("response=" + response);
        System.out.print("response=" + response);
    }

    public static void driverLogin() {
        Map<String, Object> parms = new HashMap<String, Object>();

        parms.put("phoneNo", "13641380610");
        //parms.put("phoneNo", "18286088701");

        parms.put("pwd", "111111");
        //parms.put("pwd", "123456");
        Map<String, Object> response = requestService("driver", "login", parms);
        System.out.print("response=" + response);
    }

    public static void zxpcUserPublish() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("carRank", "舒适型5座");
        parms.put("endLat", "26.592");
        parms.put("endLocation", "贵阳市 渔安派出所");
        parms.put("endLon", "106.7473");
        parms.put("goTime", "2017-03-11 12:30:00");
        parms.put("openId", "e2f8c0ae7ef1e0cf243da72ab7ec0bbb");
        parms.put("roadId", 2);
        parms.put("startLat", "26.5915");
        parms.put("startLocation", "贵阳市 渔安新城B·C组团");
        parms.put("startLon", "106.74951");
        Map<String, Object> response = requestService("zxpc", "userPublish", parms);
        System.out.print("response=" + response);
    }

    public static void kcUserPublish() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("endLat", "26.579708134700226");
        parms.put("endLocation", "贵阳市 中国银行(宝山南路支行)");
        parms.put("endLon", "106.73386693327883");
        parms.put("goTime", "2017-03-29 11:09:13");
        parms.put("openId", "e2f8c0ae7ef1e0cf243da72ab7ec0bbb");
        parms.put("startLat", "26.580362406073608");
        parms.put("startLocation", "贵阳市 凯尼大厦");
        parms.put("startLon", "106.73257337334489");
        Map<String, Object> response = requestService("kc", "userPublish", parms);
        System.out.print("response=" + response);
    }

    public static void searchLostList() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "54f2d92b17173057bb5827acd38664f7");
        parms.put("searchKeys", "");
        parms.put("self", true);
        parms.put("status", 3);
        parms.put("pageIndex", 1);
        parms.put("pageSize", 10);
        Map<String, Object> response = requestService("lost", "search", parms);
        System.out.print("response=" + response);
    }

    public static void searchDriver() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "e2f8c0ae7ef1e0cf243da72ab7ec0bbb");
        parms.put("carRank", "");
        parms.put("carSeats", "");
        parms.put("lat", 26.590992);
        parms.put("lon", 106.74872);
        parms.put("serviceCode", "01");
        parms.put("pageIndex", 1);
        parms.put("pageSize", 10);
        Map<String, Object> response = requestService("driver", "search", parms);
        System.out.print("response=" + response);
    }

    public static void driverGetUserNewTripList() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "54f2d92b17173057bb5827acd38664f7");
        parms.put("serviceCode", "");
        parms.put("pageIndex", 1);
        parms.put("pageSize", 10);
        Map<String, Object> response = requestService("trip", "driverGetUserNewTripList", parms);
        System.out.print("response=" + response);
    }

    public static void zxpcDriverAccept() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "54f2d92b17173057bb5827acd38664f7");
        parms.put("tripNo", "ZX17042511333701211");
        Map<String, Object> response = requestService("zxpc", "driverAccept", parms);
        System.out.print("response=" + response);
    }

    public static void driverRefuse() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "54f2d92b17173057bb5827acd38664f7");
        parms.put("reason", "图");
        parms.put("tripNo", "ZX17031613223197876");
        Map<String, Object> response = requestService("trip", "driverRefuse", parms);
        System.out.print("response=" + response);
    }

    public static void userGetTripList() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "e2f8c0ae7ef1e0cf243da72ab7ec0bbb");
        parms.put("serviceCode", "");
        parms.put("status", "");
        parms.put("pageIndex", 1);
        parms.put("pageSize", 10);
        Map<String, Object> response = requestService("trip", "userGetTripList", parms);
        System.out.print("response=" + response);
    }

    public static void driverGetTripCrewsList() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "54f2d92b17173057bb5827acd38664f7");
        parms.put("tripNo", "TP17042514451801296");
        Map<String, Object> response = requestService("trip", "driverGetTripCrewsList", parms);
        System.out.print("response=" + response);
    }

    public static void zxpcDriverSureUserOnCar() {

        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "54f2d92b17173057bb5827acd38664f7");
        parms.put("driverTripNo", "TP17031918425816601");
        parms.put("userTripNo", "ZX17031918415730638");
        Map<String, Object> response = requestService("trip", "driverSureUserOnCar", parms);
        System.out.print("response=" + response);
    }

    public static void bcDriverAccept() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "54f2d92b17173057bb5827acd38664f7");
        parms.put("tripNo", "BC17031918422744748");
        Map<String, Object> response = requestService("bc", "driverAccept", parms);
        System.out.print("response=" + response);
    }

    public static void driverCompleteTrip() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "54f2d92b17173057bb5827acd38664f7");
        parms.put("tripNo", "TP17040511275741856");
        Map<String, Object> response = requestService("trip", "driverCompleteTrip", parms);
        System.out.print("response=" + response);
    }

    public static void estimateFee() {

        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "d8d313fc6b7d93bf490828a89ab5e56b");
        parms.put("carType", "");
        parms.put("roadId", 0);
        parms.put("crewCount", 1);
        parms.put("serviceCode", "03");
        parms.put("startLon", "107.618924");
        parms.put("startLat", "28.868629");
        parms.put("endLon", "107.613319");
        parms.put("endLat", "28.852179");

        Map<String, Object> response = requestService("trip", "estimateFee", parms);


        System.out.print("response=" + response);
    }

    public static void zxpcUserCancel() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "73ac7dbe6ced403850c48bcd74356931");
        parms.put("tripNo", "ZX17032900150213513");

        Map<String, Object> response = requestService("zxpc", "userCancel", parms);
        System.out.print("response=" + response);
    }

    public static void userPayTripDeposit() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "e2f8c0ae7ef1e0cf243da72ab7ec0bbb");
        parms.put("tripNo", "ZX17032423161930692");
        parms.put("payId", 3);
        parms.put("ip", "192.168.1.100");

        Map<String, Object> response = requestService("trip", "userPayTripDeposit", parms);
        System.out.print("response=" + response);
    }

    public static void driverCanAcceptUserTripList() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "24f2d92b17173057bb5827acd38664f0");
        parms.put("serviceCode", "");
        parms.put("pageIndex", 1);
        parms.put("pageSize", 5);

        Map<String, Object> response = requestService("trip", "driverCanAcceptUserTripList", parms);
        System.out.print("response=" + response);
    }

    public static void userComment() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "01ce670c622b097ef13cfe3ea8336ff1");
        parms.put("tripNo", "KC17042018313691897");
        parms.put("star", 5);
        parms.put("content", "很好，很不错");

        Map<String, Object> response = requestService("trip", "userComment", parms);
        System.out.print("response=" + response);
    }

    public static void userComplaint() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "e2f8c0ae7ef1e0cf243da72ab7ec0bbb");
        parms.put("tripNo", "BC17040514361694927");
        parms.put("content", "态度太差");

        Map<String, Object> response = requestService("trip", "userComplaint", parms);
        System.out.print("response=" + response);
    }

    public static void userRecharge() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "e2f8c0ae7ef1e0cf243da72ab7ec0bbb");
        parms.put("rechargeMoney", "0.01");
        parms.put("giveMoney", "1000.0");
        parms.put("ip", "192.168.1.100");
        parms.put("payId", "2");

        Map<String, Object> response = requestService("user", "recharge", parms);
        System.out.print("response=" + response);
    }

    public static void smsCheckExpired() {
        Map<String, Object> parms = new HashMap<String, Object>();
        //parms.put("phoneNo", "18286088701");
        parms.put("phoneNo", "15120197446");

        //parms.put("validCode", "374042");
        parms.put("validCode", "732075");

        Map<String, Object> response = requestService("sms", "checkExpired", parms);
        System.out.print("response=" + response);
    }

    public static void sendSms() {

        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("phoneNo", "15285566816");

        Map<String, Object> response = requestService("sms", "getValidCode", parms);
        System.out.print("response=" + response);
//		SmsConfig config = SmsConfig.newBuilder().setPlatform(SmsPlatform.AliDayu)
//				 .setAccessKey("23749306")
//				 .setAccessPwd("eed225a68792fb6e0fe57e83712f9712")
//				 .setSignName(Consts.SystemName)
//				 .setTempleteCode(AliDayu.TEMPLETE_VALICODE)
//				 .setPhonNos("15285566816")
//				 .setContent("{\"code\":\"123456\"}")
//				 .build();
//		Boolean sended = SmsFactory.send(config);
//		System.out.print("sended="+sended);
    }

    public static void setPayPwd() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "2eb511fb2c4a527a030cf0f096ff963a");
        parms.put("payPwd", "123456");
        parms.put("validCode", "634522");

        Map<String, Object> response = requestService("user", "setPayPwd", parms);
        System.out.print("response=" + response);
    }

    public static void aliRefund() {
        String tradeNo = "2017032921001004100268466040";
        String response = HttpUtil.get("http://127.0.0.1:8080/deposit/paynotify/alipay/refund", "tradeNo=" + tradeNo, null);
        System.out.print("response=" + response);
    }


    public static void getCarRanks() {
        Map<String, Object> parms = new HashMap<String, Object>();
        parms.put("openId", "73ac7dbe6ced403850c48bcd74356931");

        Map<String, Object> response = requestService("trip", "getCarRanks", parms);
        System.out.print("response=" + response);
    }

    public static void baiduDriving() {
        //两点间路线距离估算
        RouteMatrixRequestParms request = new RouteMatrixRequestParms.Builder()
                .setOrigins("40.45,116.34|40.54,116.35")
                .setDestinations("40.34,116.45|40.35,116.46")
                .setOutput("json")
                .setTactics("11")
                .setAk("RThzSLZyjXZsEs5V4mqUILDudwpPtZo1")
                .build();
        RouteMatrixResponseParmsResult result = RouteMatrixApi.driving(request);
        System.out.print("result=" + result);
    }

    /**
     * 获取接口的请求对象Map
     *
     * @param module
     * @param method
     * @param parms
     * @return
     */
    protected static Map<String, Object> getServiceRequestObject(String module, String method, Map<String, Object> parms) {
        Map<String, Object> requestMap = new HashMap<String, Object>();
        requestMap.put("module", module);
        requestMap.put("method", method);
        requestMap.put("version", 1);
        requestMap.put("token", "");
        requestMap.put("os", "ios");
        requestMap.put("platform", "client");

        requestMap.put("parms", parms);

        return requestMap;
    }

    /**
     * 请求接口
     *
     * @param module 模块
     * @param method 方法
     * @param parms  参数
     * @return
     */
    protected static Map<String, Object> requestService(String module, String method, Map<String, Object> parms) {
        Map<String, Object> requestMap = getServiceRequestObject(module, method, parms);
        String json = JSON.toJSONString(requestMap);
        String sign = SignUtil.encrypt(json, 1, "ldk@api");

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("sign", sign);

        String response = HttpUtil.post(getServiceUrl(), json, headers);

        Map<String, Object> responseObject = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(response)) {
            responseObject = JSON.parseObject(response, Map.class);
        }
        return responseObject;
    }

    /**
     * 请求接口
     *
     * @param requestMap
     * @return
     */
    protected static Map<String, Object> requestService(Map<String, Object> requestMap) {
        String json = JSON.toJSONString(requestMap);
        String sign = SignUtil.encrypt(json, 1, "ldk@api");


        Map<String, Object> responseObject = requestService(json, sign);

        return responseObject;
    }

    /**
     * 请求接口
     *
     * @param requestMap
     * @return
     */
    protected static Map<String, Object> requestService(String json, String sign) {

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("sign", sign);

        String respone = HttpUtil.post(getServiceUrl(), json, headers);

        Map<String, Object> responseObject = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(respone)) {
            responseObject = JSON.parseObject(respone, Map.class);
        }
        return responseObject;
    }

    /**
     * 生成指定位数n的随机数
     * 生成6位司机ID 或 8位乘客ID
     * @param requestMap
     * @return
     */
    protected static void generateID(int n) {
        double base = Math.pow(10, n-1);
        int id = (int) ((Math.random() * 9 + 1) * base);
        System.out.println("id=" + id);
    }

    private static String getServiceUrl() {
        return "http://127.0.0.1:8080/request/post";//"http://app.fgldkd.com/request/post";//"http://127.0.0.1:8080/request/post";
    }
}
